﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace PolePositionMobileUtilityGeneral
{
    public static class Utils
    {
        #region DB
        /// <summary>
        /// Opens and returns a sql connection
        /// </summary>
        public static SqlCeConnection GetConnection()
        {
            return GetConnection(PPIConstants.K_ConnectionString);
        }
        public static SqlCeConnection GetConnection(string connString)
        {
            //Created by: VMCoder
            //Creation date: 2008.04.21
            //Last updated by: VMCoder
            //Last update: 2008.04.21

            try
            {
                if (connString.IndexOf("Data Source") < 0)
                    connString = "Data Source = " + connString + ";";
                SqlCeConnection _Con = new SqlCeConnection(connString);
                if (_Con.State != ConnectionState.Open)
                    _Con.Open();

                return _Con;
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// Creates and returns a sql parameter
        /// </summary>
        public static SqlCeParameter CreateParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return new SqlCeParameter(parameterName, dbType, size, direction, isNullable, precision, scale, String.Empty, DataRowVersion.Default, value);
        }
        /// <summary>
        /// Creates and returns a sql Int parameter
        /// </summary>
        public static SqlCeParameter CreateIntParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.Int, 0, direction, isNullable, 0, 0, value);
        }
        /// <summary>
        /// Creates and returns a sql Bit parameter
        /// </summary>
        public static SqlCeParameter CreateBitParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.Bit, 0, direction, isNullable, 0, 0, value);
        }
        /// <summary>
        /// Creates and returns a sql DateTime parameter
        /// </summary>
        public static SqlCeParameter CreateDateTimeParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.DateTime, 0, direction, isNullable, 0, 0, value);
        }
        /// <summary>
        /// Creates and returns a sql NVarChar parameter
        /// </summary>
        public static SqlCeParameter CreateNVarCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.NVarChar, size, direction, isNullable, 0, 0, value);
        }
        /// <summary>
        /// Creates and returns a sql Decimal parameter
        /// </summary>
        public static SqlCeParameter CreateDecimalParameter(string parameterName, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            //Created by: -
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            return CreateParameter(parameterName, SqlDbType.Decimal, 0, direction, isNullable, precision, scale, value);
        }

        #endregion DB

    }

    public static class ConvertMobileHelper
    {
        #region Conversion
        /// <summary>
        /// Converts an object to Int32 or returns Int32.MinValue if it is null or DBNull
        /// </summary>
        public static Int32 ToInt32(Object obj)
        {
            //Created by: VMCoder
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            try
            {
                return obj != null && obj != DBNull.Value ? Int32.Parse(obj.ToString()) : Int32.MinValue;
            }
            catch
            {
                return Int32.MinValue;
            }
        }
        /// <summary>
        /// Converts an object to double or returns double.MinValue if it is null or DBNull
        /// </summary>
        public static double Todouble(Object obj)
        {
            //Created by: VMCoder
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            try
            {
                return obj != null && obj != DBNull.Value ? double.Parse(obj.ToString()) : double.MinValue;
            }
            catch
            {
                return double.MinValue;
            }
        }
        /// <summary>
        /// Converts an object to DateTime or returns DateTime.MinValue if it is null or DBNull
        /// </summary>
        public static DateTime ToDateTime(Object obj)
        {
            //Created by: VMCoder
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            try
            {
                return obj != null && obj != DBNull.Value ? DateTime.Parse(obj.ToString()) : DateTime.MinValue;
            }
            catch
            {
                return DateTime.MinValue;
            }
        }

        public static string Tostring(object obj)
        {
            try
            {
                return obj.ToString();
                
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// Transforms longitude from GPS format to a decimal number.
        /// </summary>
        public static double ConvertLongitudeToDecimal(string Position, string CoordinatedLetter, ref string FormatedView)
        {
            //Created by: GBarzola
            //Creation date: -
            //Last updated by: GBarzola
            //Last update: -

            try
            {
                double SingleValue = 1;
                switch (CoordinatedLetter)
                {
                    case "E":
                        SingleValue = 1;
                        break;
                    case "W":
                        SingleValue = -1;
                        break;
                }
                double NewPosition = 0;
                FormatedView = "0";
                if (Position != "0")
                {
                    string[] arrPosition = Position.Split('.');
                    double previewMinutes = double.Parse(arrPosition[0].Substring(3, 2));
                    double previewDegree = double.Parse(arrPosition[0].Substring(0, 3));

                    double previewSeconds = (double.Parse(arrPosition[1]) / 10000000) * 60;
                    FormatedView = previewDegree.ToString() + " ° " + previewMinutes.ToString() + " ' " + previewSeconds.ToString() + " \" ";
                    // 34º 36' 34.79'' = 34 + ( 36 / 60 ) + ( 34,79 / 3600 ) =  (expresado en decimal)
                    NewPosition = (previewDegree + (previewMinutes / 60) + (previewSeconds / 3600)) * SingleValue;
                }
                return NewPosition;
            }
            catch (Exception ex)
            {
                return 0;
            }


        }
        /// <summary>
        /// Transforms latitude from GPS format to a decimal number.
        /// </summary>
        public static double ConvertLatitudeToDecimal(string Position, string CoordinatedLetter, ref string DegreeFormated)
        {
            //Created by: GBarzola
            //Creation date: -
            //Last updated by: GBarzola
            //Last update: -

            try
            {
                double SingleValue = 1;
                switch (CoordinatedLetter)
                {
                    case "N":
                        SingleValue = 1;
                        break;
                    case "S":
                        SingleValue = -1;
                        break;
                }
                double NewPosition = 0;
                DegreeFormated = "0";
                if (Position != "0")
                {
                    string[] arrPosition = Position.Split('.');
                    double previewMinutes = double.Parse(arrPosition[0].Substring(2, 2));
                    double previewDegree = double.Parse(arrPosition[0].Substring(0, 2));
                    double previewSeconds = (double.Parse(arrPosition[1]) / 10000000) * 60;
                    DegreeFormated = previewDegree.ToString() + " ° " + previewMinutes.ToString() + " ' " + previewSeconds.ToString() + " \" ";
                    // 34º 36' 34.79'' = 34 + ( 36 / 60 ) + ( 34,79 / 3600 ) =  (expresado en decimal)
                    NewPosition = (previewDegree + (previewMinutes / 60) + (previewSeconds / 3600)) * SingleValue;
                }
                return NewPosition;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        #endregion Conversion
    }


    public static class MobileUIHelper
    {
        public static List<Int32> FillDropdown(ComboBox dropDown, object[] dataSource, string dataTextField, string dataValueField, string additionalItem)
        {
            string _name = "";
            Int32 _value = 0;
            try
            {
                List<Int32> _returnValues = new List<Int32>();
                dropDown.BeginUpdate();
                dropDown.Items.Clear();

                if (!String.IsNullOrEmpty(additionalItem))
                {
                    _returnValues.Add(0);
                    dropDown.Items.Add(additionalItem);
                }
                for (int i = 0; i < dataSource.Length; i++)
                {
                    Object obj = dataSource[i];
                    _name = obj.GetType().GetProperty(dataTextField).GetValue(obj, null).ToString();
                    _value = (Int32)obj.GetType().GetProperty(dataValueField).GetValue(obj, null);
                    _returnValues.Add(_value);
                    dropDown.Items.Add(_name);
                }
                dropDown.EndUpdate();
                if (dropDown.Items.Count > 0)
                    dropDown.SelectedIndex = 0;
                return _returnValues;
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Fills a combo with numeric values from a given range, with or without starting "[None]" option
        /// </summary>
        public static void FillNumericCombo(ComboBox combo, Int32 startingValue, Int32 endingValue, Int32 step, bool addNoneOption, string noneOption)
        {
            //Created by: VMCoder
            //Creation date: -
            //Last updated by: VMCoder
            //Last update: 2008.04.10

            combo.BeginUpdate();
            combo.Items.Clear();
            if (addNoneOption)
                combo.Items.Add(noneOption);
            if (endingValue > startingValue)
                for (Int32 _I = startingValue; _I <= endingValue; _I += step)
                    combo.Items.Add(_I);
            else
                for (Int32 _I = startingValue; _I >= endingValue; _I -= step)
                    combo.Items.Add(_I);
            if (combo.Items.Count > 0)
                combo.SelectedIndex = 0;
            combo.EndUpdate();
        }

        public static List<Int32> FillDropdownYears(ComboBox dropDown, int startYear, int endYear, string additionalItem)
        {
            try
            {
                List<Int32> _returnValues = new List<Int32>();
                dropDown.BeginUpdate();
                dropDown.Items.Clear();

                if (!String.IsNullOrEmpty(additionalItem))
                {
                    _returnValues.Add(0);
                    dropDown.Items.Add(additionalItem);
                }
                while (endYear >= startYear)
                {
                    _returnValues.Add(endYear);
                    dropDown.Items.Add(endYear.ToString());
                    endYear -= 1;
                }
                dropDown.EndUpdate();
                if (dropDown.Items.Count > 0)
                    dropDown.SelectedIndex = 0;
                return _returnValues;
            }
            catch
            {
                throw;
            }
        }

        public static List<Int32> FillListbox(ListBox dropDown, object[] dataSource, string dataTextField, string dataValueField, string additionalItem)
        {
            try
            {
                string _name = "";
                Int32 _value = 0;
                try
                {
                    List<Int32> _returnValues = new List<Int32>();
                    dropDown.BeginUpdate();
                    dropDown.Items.Clear();

                    if (!String.IsNullOrEmpty(additionalItem))
                    {
                        _returnValues.Add(0);
                        dropDown.Items.Add(additionalItem);
                    }
                    for (int i = 0; i < dataSource.Length; i++)
                    {
                        Object obj = dataSource[i];
                        _name = obj.GetType().GetProperty(dataTextField).GetValue(obj, null).ToString();
                        _value = (Int32)obj.GetType().GetProperty(dataValueField).GetValue(obj, null);
                        _returnValues.Add(_value);
                        dropDown.Items.Add(_name);
                    }
                    dropDown.EndUpdate();
                    if (dropDown.Items.Count > 0)
                        dropDown.SelectedIndex = 0;
                    return _returnValues;
                }
                catch
                {
                    throw;
                }
            }
            catch
            {
                throw;
            }
        }

        public static void SetSelectedValue(List<Int32> values,Int32 value, ComboBox dropdown)
        {
            try
            {
                int index;
                if (values.Contains(value))
                {
                    index = values.IndexOf(value);
                    dropdown.SelectedIndex = index;
                }
                else
                {
                    dropdown.SelectedIndex = 0;
                }
            }
            catch (Exception)
            {
                dropdown.SelectedIndex = -1;
            }
            

        }

        public static Int32 GetSelectedValue(List<Int32> values, ComboBox dropdown)
        {
            Int32 returnValue = Int32.MinValue;
            if (dropdown.SelectedIndex > 0)
            {
                returnValue = values[dropdown.SelectedIndex];
            }
            return returnValue;
        }

            
    }

}
